package com.blogSystem.user.service;


import com.blogSystem.user.dao.AdminDao;
import com.blogSystem.user.dao.UserDao;
import com.blogSystem.user.pojo.Admin;
import io.jsonwebtoken.Claims;
import org.omg.SendingContext.RunTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import com.blogSystem.user.pojo.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;
import util.JwtUtil;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SuperService  {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BCryptPasswordEncoder encoder;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JwtUtil jwtUtil;


    public List<Admin> findAll(){

        return adminDao.findAll();
    }

    /**
     * 查找
     * @param id
     * @return
     */
    public User findById(String id){
        return userDao.findById(id).get();
    }

    /**
     * 增加
     * @param
     */
    public void setAdmin(User user){
        user.setId(idWorker.nextId()+"");

        //加密
        user.setPassword(encoder.encode(user.getPassword()));
        user.setRole("ROLE_ADMIN");

        System.out.println(userDao.save(user));

    }

    /**
     * 根据名字查询
     * @param admin
     * @return
     */
    public User searchAdmin(User user){
        return userDao.findByUsernameAndRole(user.getUsername(),"ROLE_ADMIN");
    }

    public Admin login(Admin admin ){
        //现根据用户名查询对象
        Admin adminlog= adminDao.findAdminByName(admin.getName());

        //拿数据库中密码和用户密码看是否匹配
        if(adminlog!=null&&encoder.matches(admin.getPassword(),adminlog.getPassword())){
            return adminlog;
        }
        return null;
    }

    public void delete(String id){
        userDao.deleteById(id);
    }

    /**
     * 修改名字
     * @param id
     * @param newAdmin
     * @return
     */
    public boolean changeName(String id,User newAdmin){
        User oldAdmin=userDao.findById(id).get();
        if(newAdmin.getUsername()!=null&&!newAdmin.getUsername().equals("")){

            if(userDao.findByUsernameAndRole(newAdmin.getUsername(),"ROLE_ADMIN")==null){
                oldAdmin.setUsername(newAdmin.getUsername());
                userDao.save(oldAdmin);
                return true;
            }else {
                return false;
            }
        }
        return false;
    }

    /**
     * 修改密码
     * @param id
     * @param newAdmin
     * @return
     */
    public boolean changePass(String id,User newAdmin){
        User oldAdmin=userDao.findById(id).get();
        if(newAdmin.getPassword()!=null&&!newAdmin.getPassword().equals("")){
            String newPass=encoder.encode(newAdmin.getPassword());
            oldAdmin.setPassword(newPass);
            userDao.save(oldAdmin);
            return true;
        }
        return false;
    }
    /**
     * 根据名字模糊条件查询
     * @param admin
     * @return
     */
    public Page<User> findSearch(User user, int page, int size){
        Pageable pageable= PageRequest.of(page-1,size);
        return userDao.findAllByUsernameLikeAndRole(user.getUsername(),"ROLE_ADMIN",pageable);
    }
    /**
     * 查询所有
     *
     * @return
     */
    public Page<User> findAll( int page, int size){
        Pageable pageable= PageRequest.of(page-1,size);
        return userDao.findAllByRole("ROLE_ADMIN",pageable);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteList(List<String> ids){
        adminDao.deleteAdminsByIdIn(ids);
    }

  /*  @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //在mysql中查询

        try{
            Admin admin=adminDao.findAdminByName(username);
            if(admin==null){
                return null;
            }
            List<SimpleGrantedAuthority> authorities=new ArrayList<>();
            authorities.add(new SimpleGrantedAuthority("ROLE_SUPER"));
            UserDetails userDetails=new User(admin.getName(),admin.getPassword(),authorities);
            return userDetails;
        }catch (Exception e){
            return null;
        }

    }*/


}
